package br.ufmg.ubicomp.droidguide.eventservice;

import java.util.TimerTask;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import br.ufmg.ubicomp.droidguide.R;
import br.ufmg.ubicomp.droidguide.eventservice.notification.NotificationBucket;
import br.ufmg.ubicomp.droidguide.utils.AndroidUtils;


public class NotificationList extends ListActivity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Use an existing ListAdapter that will map an array
        // of strings to TextViews
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, 
                NotificationBucket.getInstance().
                getNotificationsList()));
        getListView().setItemsCanFocus(true);
        getListView().setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long id) {
				
				//showAlert(id);
			}
        	
        });
        getListView().setTextFilterEnabled(true);
        
        TimerTask task = new TimerTask(){
        	@Override
        	public void run() {
        		updateList();
        	}
        };
        
        Handler handler = new Handler();
        handler.removeCallbacks(task);
        handler.postDelayed(task, 2000);
    }	
	
	private void updateList()
	{
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, 
                NotificationBucket.getInstance().
                getNotificationsList()));
	}
	
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0,1, 0, "Clear").setIcon(R.drawable.ic_cancel)
		 .setIntent(new Intent(android.provider.Settings.ACTION_SETTINGS));
      return true;
    }
	
    @Override
    protected void onResume() {
    	super.onResume();
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, 
                NotificationBucket.getInstance().
                getNotificationsList()));
    }
    
	@Override
    public boolean onOptionsItemSelected(MenuItem item){
    	Intent i;
    	if (item.getItemId() == 0) // OK
    	{
    		NotificationBucket.getInstance().clear();
    		
            setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, 
                    NotificationBucket.getInstance().
                    getNotificationsList()));    		
    	}
        return true;
    }
    
	private void showAlert(long id)
	{
		AndroidUtils.showAlert(this, "test " + id);		
	}
}
